// pages/applyTTC/applyTTC.js
import {
  Get,
  Post,
  JsonPost,
  API_URL
} from "../../utils/request.js"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type:0,
    license_img:'',
    province:'选择装修公司地区',
    city:'',
    district:'',
    full_name:'',
    phone:'',
    name:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  radioChange(e) {
    this.setData({
      type:e.detail.value
    })
  },

  bindRegionChange: function (e) {
    this.setData({
      province: e.detail.value[0],
      city: e.detail.value[0],
      district: e.detail.value[0],
    })
  },

  
  uploadImg(){
    let that=this
    wx.chooseImage({
      success (res) {
        const tempFilePaths = res.tempFilePaths
        wx.uploadFile({
          url: API_URL+'user/image',
          filePath: tempFilePaths[0],
          name: 'upload',
          header	:{'X-Access-Token':wx.getStorageSync('token')},
          formData: {
            'upload': tempFilePaths[0]
          },
          success (re){
            that.setData({
              license_img:JSON.parse(re.data).data
            })
          }
        })
      }
    })
  },

    
  setValue(e) {
    let str = e.currentTarget.dataset.row
    this.setData({
      [str]: e.detail.value
    })
  },

  wpxSumbit(){
    if (!this.data.full_name||!this.data.phone) {
      wx.showToast({
        title: '请输入姓名和手机号',
        icon: 'none',
        duration: 2000
      })
      return
    }
    if (this.data.type==0) {
      if (!this.data.license_img||!this.data.province||!this.data.city||!this.data.district||!this.data.name) {
        wx.showToast({
          title: '请输入完整信息',
          icon: 'none',
          duration: 2000
        })
        return
      }
    }
    let obj={
      full_name:this.data.full_name,
      phone:this.data.phone,
      type:this.data.type,
      province:this.data.province,
      city:this.data.city,
      district:this.data.district,
      name:this.data.name,
      license_img:this.data.license_img,
    }
    JsonPost('shop/submit-certification',obj).then(res=>{
      if (res.code==0) {
        wx.showToast({
          title: '申请成功',
          icon: 'none',
          duration: 2000
        })
        wx.navigateBack()
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})